package com.yinet.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.yinet.common.core.domain.Stree;
import com.yinet.common.utils.StringUtils;
import com.yinet.system.domain.GuidanceFile;
import com.yinet.system.domain.TouchScreenGuidance;
import com.yinet.system.mapper.TouchScreenGuidanceMapper;
import com.yinet.system.service.IGuidanceFileService;
import com.yinet.system.service.TouchScreenGuidanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 办事指南 业务层处理
 *
 * @author ruoyi
 */
@Service
public class TouchScreenGuidanceServiceImpl implements TouchScreenGuidanceService {

    @Autowired
    private TouchScreenGuidanceMapper guidanceMapper;
    @Autowired
    private IGuidanceFileService guidanceFileService;

    @Override
    public List<TouchScreenGuidance> selectGuidanceList() {
        List<TouchScreenGuidance> guidanceList = guidanceMapper.selectGuidanceList();
        return guidanceList;
    }

    @Override
    public TouchScreenGuidance selectGuidanceById(String id) {
        TouchScreenGuidance touchScreenGuidance = guidanceMapper.selectGuidanceById(id);
        List<GuidanceFile> guidanceFiles = guidanceFileService.selectGuidanceFileByGuidanceId(id);
        Map<String, List<Map<String, String>>> maps = new HashMap();
        String[] types = {"Z", "S", "K"};
        for (String str : types) {
            List<Map<String, String>> list = new ArrayList<>();
            for (GuidanceFile item : guidanceFiles) {
                if (item.getFileType().equals(str)) {
                    Map<String, String> map = new HashMap<>();
                    map.put("id", item.getFileId());
                    map.put("name", item.getFileName());
                    map.put("url", item.getFileUrl());
                    list.add(map);
                }
            }
            maps.put(str, list);
        }
        Gson gson = new Gson();
        touchScreenGuidance.setFiles(gson.toJson(maps));
        return touchScreenGuidance;
    }

    @Override
    public int updateGuidance(TouchScreenGuidance guidance, String delIds) {
        Map<String, List<Map<String, String>>> map = (Map<String, List<Map<String, String>>>) JSON.parse(guidance.getFiles());
        if (guidance.getGuidanceType().equals("W")) {
            for (Map.Entry<String, List<Map<String, String>>> entry : map.entrySet()) {
                String mapKey = entry.getKey();
                List<Map<String, String>> mapValue = entry.getValue();
                for (Map<String, String> stringMap : mapValue) {
                    GuidanceFile guidanceFile = new GuidanceFile();
                    guidanceFile.setFileId(UUID.randomUUID().toString().replaceAll("-", ""));
                    guidanceFile.setDelFlag("0");
                    guidanceFile.setFileName(stringMap.get("name"));
                    guidanceFile.setFileUrl(stringMap.get("url"));
                    guidanceFile.setFileType(mapKey);
                    guidanceFile.setGuidanceId(guidance.getTsGuidanceId());
                    guidanceFileService.insertGuidanceFile(guidanceFile);
                }
            }
        }
        if (StringUtils.isNotEmpty(delIds)) {
            guidanceFileService.deleteGuidanceFileByIds(delIds);
        }
        int i = guidanceMapper.updateGuidance(guidance);
        return i;
    }

    @Override
    public int insertGuidance(TouchScreenGuidance guidance) {
        guidance.setTsGuidanceId(UUID.randomUUID().toString().replaceAll("-", ""));
        Map<String, List<Map<String, String>>> map = (Map<String, List<Map<String, String>>>) JSON.parse(guidance.getFiles());
        if (guidance.getGuidanceType().equals("W")) {
            for (Map.Entry<String, List<Map<String, String>>> entry : map.entrySet()) {
                String mapKey = entry.getKey();
                List<Map<String, String>> mapValue = entry.getValue();
                for (Map<String, String> stringMap : mapValue) {
                    GuidanceFile guidanceFile = new GuidanceFile();
                    guidanceFile.setFileId(UUID.randomUUID().toString().replaceAll("-", ""));
                    guidanceFile.setDelFlag("0");
                    guidanceFile.setFileName(stringMap.get("name"));
                    guidanceFile.setFileUrl(stringMap.get("url"));
                    guidanceFile.setFileType(mapKey);
                    guidanceFile.setGuidanceId(guidance.getTsGuidanceId());
                    guidanceFileService.insertGuidanceFile(guidanceFile);
                }
            }
        }
        int i = guidanceMapper.insertGuidance(guidance);
        return i;
    }

    @Override
    public int selectCountGuidanceByParentId(String parentId) {
        int i = guidanceMapper.selectCountGuidanceByParentId(parentId);
        return i;
    }

    @Override
    public int deleteGuidanceById(String guidanceId) {
        int i = guidanceMapper.deleteGuidanceById(guidanceId);
        return i;
    }

    @Override
    public List<Stree> guidanceTreeData() {
        List<TouchScreenGuidance> guidanceList = guidanceMapper.selectGuidanceList();
        List<Stree> strees = initZtree(guidanceList);
        return strees;
    }

    @Override
    public List<TouchScreenGuidance> selectGuidanceListByIdAndType(String id, String type) {
        List<TouchScreenGuidance> guidanceList = guidanceMapper.selectGuidanceList();
        return findChildGuidance(id, type, guidanceList, true);
    }

    @Override
    public List<TouchScreenGuidance> selectGuidanceListByParentId(String id) {
        return guidanceMapper.selectGuidanceByParentId(id);
    }

    @Override
    public List<TouchScreenGuidance> selectGuidanceListById(String id) {
        List<TouchScreenGuidance> childList = guidanceMapper.selectGuidanceByParentId(id);
        List<TouchScreenGuidance> AllList = guidanceMapper.selectGuidanceNotParentId(id);
        for (int i = 0; i < childList.size(); i++) {
            List<TouchScreenGuidance> temp = new ArrayList<>();
            for (int j = 0; j < AllList.size(); j++) {
                if (AllList.get(j).getParentId().equals(childList.get(i).getTsGuidanceId())) {
                    temp.add(AllList.get(j));
                }
            }
            childList.get(i).setChildren(temp);
        }
        return childList;
    }

    /**
     * 对象转菜单树
     *
     * @param guidanceList 菜单列表
     * @return 树结构列表
     */
    public List<Stree> initZtree(List<TouchScreenGuidance> guidanceList) {
        List<Stree> strees = new ArrayList<Stree>();
        for (TouchScreenGuidance guidance : guidanceList) {
            if (guidance.getGuidanceType().equals("M")) {
                Stree stree = new Stree();
                stree.setId(guidance.getTsGuidanceId());
                stree.setpId(guidance.getParentId());
                stree.setName(guidance.getGuidanceName());
                stree.setTitle(guidance.getGuidanceName());
                strees.add(stree);
            }
        }
        return strees;
    }

    /**
     * 根据GuidanceId和Type获取所有子节点
     */
    public List<TouchScreenGuidance> findChildGuidance(String id, String type, List<TouchScreenGuidance> list, boolean isFirst) {
        List<TouchScreenGuidance> tempList = new ArrayList<>();
        for (TouchScreenGuidance item : list) {//循环遍历父ID为id的项
            String parentId = item.getParentId();
            String nowType = item.getGuidanceType();
            if (isFirst) {
                if (parentId.equals(id)) {//如果是子项就添加进列表
                    if (nowType.equals("M")) {
                        List<TouchScreenGuidance> childGuidance = findChildGuidance(item.getTsGuidanceId(), type, list, false);
                        if (childGuidance.size() > 0) {
                            item.setChildren(childGuidance);
                            tempList.add(item);
                        }
                    } else if (nowType.equals(type)) {
                        tempList.add(item);
                    }
                }
            } else {
                if (parentId.equals(id) && nowType.equals(type)) {//如果是子项就添加进列表
                    item.setChildren(findChildGuidance(item.getTsGuidanceId(), type, list, false));
                    tempList.add(item);
                }
            }

        }
        return tempList;
    }

    @Override
    public List<TouchScreenGuidance> selectParentGuidanceList() {
        return guidanceMapper.selectParentGuidanceList();
    }

}
